package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolListDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping
    public Result list(BusinessListDTO dto){
        log.info("查询商机列表：{}", dto);
        PageResult<BusinessDO> result = businessService.businessList(dto);
        return Result.success(result);
    }

    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO dto){
        log.info("添加商机：{}", dto);
        businessService.addBusiness(dto);
        return Result.success();
    }

    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateById(@PathVariable Integer businessId, @PathVariable Integer userId){
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id){
        log.info("根据ID查询商机详细信息, id: {}", id);
        BusinessVO vo = businessService.getBusinessById(id);
        return Result.success(vo);
    }

    @PutMapping
    public Result updateBusinessInfoById(@RequestBody UpdateBusinessInfoDTO dto){
        log.info("更新商机信息：{}", dto);
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }

    //踢回公海
    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        businessService.backToPool(id);
        return Result.success();
    }
    //商机转客户
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }
    //公海池列表查询
    @GetMapping("/pool")
    public Result businessPoolList(BusinessPoolListDTO dto) {
        PageResult<Business> pageResult = businessService.businessPoolList(dto);
        return Result.success(pageResult);
    }

}